{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copyright (2024) Bytedance Ltd. and/or its affiliates\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "import json\n",
    "import os\n",
    "\n",
    "# This is an example for DREAM\n",
    "\n",
    "def vid2path(video_dir, vid, dataset=\"DREAM-1K\"):\n",
    "    if dataset == \"DREAM-1K\":\n",
    "        # For DREAM-1K, vid is the video filename without extension\n",
    "        return f\"{video_dir}/{vid}.mp4\"\n",
    "    if dataset == \"TVBench\":\n",
    "        # For TVBench, vid is the video filename with sub dir\n",
    "        return f\"{video_dir}/{vid}\"\n",
    "    else:\n",
    "        # For other benchmarks, you should checkout their metadata and rewrite this function\n",
    "        raise NotImplementedError(f\"Unknown dataset: {dataset}\")\n",
    "    \n",
    "\n",
    "def fill_in_video_file(input_file, video_dir, dataset):\n",
    "    data = [json.loads(line) for line in open(input_file)]\n",
    "    for d in data:\n",
    "        vid = str(d['vid'])\n",
    "        video_file = vid2path(video_dir, vid, dataset)\n",
    "        assert os.path.exists(video_file)\n",
    "        for msg in d['messages']:\n",
    "            if msg['role'] == \"user\":\n",
    "                for content in msg['content']:\n",
    "                    if content['type'] == 'video':\n",
    "                        content['video']['video_file'] = video_file\n",
    "    with open (input_file, 'w') as fout:\n",
    "        for d in data:\n",
    "            fout.write(json.dumps(d, ensure_ascii=False) + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# prepare DREAM-1K\n",
    "video_dir = \"DIR_TO_DREAM_VIDEOS\"\n",
    "input_file = \"annotations/DREAM-1k.jsonl\"\n",
    "fill_in_video_file(input_file, video_dir, dataset=\"DREAM-1K\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# prepare TVBench\n",
    "# video_dir = \"DIR_TO_TVBENCH_VIDEOS\"\n",
    "video_dir = \"/mnt/bn/writing-model-training/videos/TVBench/videos\"\n",
    "input_file = \"annotations/TVBench.jsonl\"\n",
    "fill_in_video_file(input_file, video_dir, dataset=\"TVBench\")"
   ]
  }
 ],
 "metadata": {
  "fileId": "e59bc997-e8ba-417e-b6ae-d3b59b724da7",
  "filePath": "/mnt/bn/videonasi18n/wangjw/workspace/tarsier/data/fill_in_video_file.ipynb",
  "kernelspec": {
   "display_name": "Python 3.9.2 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
